package com.yxt.dispatch.service.impl;

import com.yxt.common.utils.ajax.PageUtils;
import com.yxt.common.utils.ajax.Query;
import com.yxt.dispatch.entity.AreaEntity;
import com.yxt.dispatch.entity.FormEntity;
import com.yxt.dispatch.entity.FormItemEntity;
import com.yxt.dispatch.mapper.AreaMapper;
import com.yxt.dispatch.mapper.FormItemMapper;
import com.yxt.dispatch.mapper.FormMapper;
import com.yxt.dispatch.service.FormItemService;
import com.yxt.dispatch.vo.FormItemVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


@Service("formItemService")
public class FormItemServiceImpl extends ServiceImpl<FormItemMapper, FormItemEntity> implements FormItemService {

    @Autowired
    private AreaMapper areaMapper;

    @Autowired
    private FormMapper formMapper;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<FormItemEntity> page = this.page(
                new Query<FormItemEntity>().getPage(params),
                new QueryWrapper<FormItemEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<FormItemVO> formItem(String areaName, String formName) {
        //得到area
        AreaEntity areaEntity = areaMapper.selectOne(new QueryWrapper<AreaEntity>().eq("area_name", areaName));
        //得到form
        FormEntity formEntity = formMapper.selectOne(new QueryWrapper<FormEntity>()
                .eq("form_name", formName)
                .eq("area_id", areaEntity.getAreaId()));

        //得到formItem
        List<FormItemEntity> list = this.baseMapper.selectList(new QueryWrapper<FormItemEntity>().eq("form_id", formEntity.getFormId()));

        List<FormItemVO> formvos = list.stream().map(item -> {
            FormItemVO formVO = new FormItemVO();
            formVO.setLabel(item.getItemLabel());
            formVO.setModel(item.getItemModel());
            formVO.setType(item.getItemType());
            if(StringUtils.isNotBlank(item.getItemOpt())){
                formVO.setOpts(item.getItemOpt());
            }
            return formVO;
        }).collect(Collectors.toList());


        return formvos;
    }

}